package 英译汉;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UdpEchoClient {
    private DatagramSocket socket=null;
    private String serverip;
    private int port;
    public UdpEchoClient(String serverip,int port) throws SocketException {
        this.socket = new DatagramSocket();
        this.port=port;
        this.serverip=serverip;
    }
    public void start() throws IOException {
        while (true){
            Scanner scanner=new Scanner(System.in);
            String str=scanner.next();
            if(str.equals("exit")){
                return;
            }
            DatagramPacket packet=new DatagramPacket(str.getBytes(),str.getBytes().length,
                                                    InetAddress.getByName(serverip),port);
            socket.send(packet);
            DatagramPacket responsepacket=new DatagramPacket(new byte[4096],4096);
            socket.receive(responsepacket);
            String s=new String(responsepacket.getData(),0,responsepacket.getLength());
            String log=String.format("req:%s,response:%s",str,s);
            System.out.println(log);
        }
    }

    public static void main(String[] args) throws IOException {
        UdpEchoClient udpEchoClient=new UdpEchoClient("127.0.0.1",9090);
        udpEchoClient.start();
    }
}
